//
//  ByUIMineSettingsVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIMineSettingsVC.h"
#import "ByUILoginVC.h"
@interface ByUIMineSettingsVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *titlesAry;
@property (nonatomic, copy) NSString *cacheStr;
@end

@implementation ByUIMineSettingsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self addLeftBackBarButtonItem];
    [self initTableView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self countCacheSize];
}

#pragma mark -- getter
-(NSArray *)titlesAry{
    if (!_titlesAry) {
        _titlesAry = @[@"当前版本",@"关于我们",@"清除缓存"];
    }
    return _titlesAry;
}

#pragma mark -- 创建视图
- (void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60 + 20)];
    UIButton *quitBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10 + 20, SCREEN_WIDTH - 20, 50)];
    [quitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    quitBtn.backgroundColor = BY_THEME_COLOR;
    [quitBtn addTarget:self action:@selector(quitAction:) forControlEvents:UIControlEventTouchUpInside];
    quitBtn.layer.cornerRadius = 4.0;
    [footerView addSubview:quitBtn];
    _tableView.tableFooterView = footerView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesAry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"setCel";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    for (UIView *subview in cell.subviews) {
        [subview removeFromSuperview];
    }
    CGFloat x,y,width,height;
    x = 10;
    width = SCREEN_WIDTH - 2*x;
    height = 50;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
    bgView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    [cell addSubview:bgView];
    
    x = 10;
    y = 15;
    width = [self.titlesAry[0] getSizeOfString:14].width;
    height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = self.titlesAry[indexPath.row];
    [bgView addSubview:titleLabel];
    if (indexPath.row != 2) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 49, bgView.width - 20, 0.5)];
        lineView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:lineView];
    }
    
    // 当前版本
    if (indexPath.row == 0) {
        width = bgView.width - titleLabel.width - 20;
        x = titleLabel.right;
        UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        versionLabel.font = FONT(14);
        versionLabel.textColor = [UIColor colorWithWhite:0.90 alpha:1];
        versionLabel.text = APP_VERSION;
        versionLabel.textAlignment = NSTextAlignmentRight;
        [bgView addSubview:versionLabel];
        
        [self paintcorner:UIRectCornerTopLeft | UIRectCornerTopRight view:bgView];

    }
    // 关于我们
    else if (indexPath.row == 1){
        x = bgView.width - 10 - 20;
        UIImageView *arrowImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 20, 20)];
        arrowImg.image = [UIImage imageNamed:@"mine_arrow"];
        [bgView addSubview:arrowImg];
    
    }
    // 缓存
    else{
        width = bgView.width - titleLabel.width - 20;
        x = titleLabel.right;
        UILabel *cacheLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        cacheLabel.font = FONT(14);
        cacheLabel.textColor = [UIColor colorWithWhite:0.90 alpha:1];
        cacheLabel.text = self.cacheStr;
        cacheLabel.textAlignment = NSTextAlignmentRight;
        [bgView addSubview:cacheLabel];

        [self paintcorner:UIRectCornerBottomLeft | UIRectCornerBottomRight view:bgView];
    }
   
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 关于我们
    if (indexPath.row == 1) {
//        [ByCoreToolAlert showOnlyText:@"关于我们"];
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@staticpage/about",BOYE_WEB_BASE_URL]];
        vc.title = @"关于我们";
        [self by_normalPush:vc hasNavi:YES];
        
    }
    // 清除缓存
    else if (indexPath.row == 2){
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"清除缓存" message:@"确定要清除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
    return;
}

#pragma mark -- 退出登录
- (void)quitAction:(UIButton *)button{
    [ByCacheUser setUserInfo:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- 画边角
- (void)paintcorner:(UIRectCorner)coners view:(UIView *)view{
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:coners cornerRadii:CGSizeMake(10, 10)];
    maskLayer.path = maskPath.CGPath;
    maskLayer.frame = view.bounds;
    view.layer.mask = maskLayer;
}

#pragma mark -- 清除缓存
//alertView 点击方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1){
        //清除缓存
        [[SDImageCache sharedImageCache] clearDisk];
        [self performSelector:@selector(countCacheSize) withObject:nil afterDelay:0.1];
    }
}
//计算缓存
-(void)countCacheSize
{
    //计算缓存大小
    NSUInteger fileSize = [[SDImageCache sharedImageCache] getSize];
    
    //打印缓存存储地址
    //    NSLog(@"%@",NSHomeDirectory());
    //    unsigned long long size =
    //    [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"CustomFile"];
    //转换为兆
    self.cacheStr = [NSString stringWithFormat:@"%.2fM",fileSize/1024.0/1024.0];
    
    [self.tableView reloadData];
}

@end
